// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   DefaultDBConnection.java

package cn.org.zx.db.dbconnection.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.org.zx.db.dbconnection.DBConnection;
import cn.org.zx.db.dbexception.DBErrorException;

public abstract class DefaultDBConnection implements DBConnection {
    protected Connection connection;
    protected Statement stmt;
    protected DataSource ds;

    public DefaultDBConnection()
    { 
        connection = null;
        stmt = null;
        ds = null;
    }
    @Override
    public Connection getConnection(){
        return connection;
    }
    @Override
    public void openbyJNDI(String jndiName)throws DBErrorException {
        try{
            InitialContext context = new InitialContext();
            ds = (DataSource)context.lookup(jndiName);
            connection = ds.getConnection();
            stmt = connection.createStatement();
        }
        catch(NamingException e)
        {
            e.printStackTrace();
            throw new DBErrorException("jndi连接出错：jndiname:[" + jndiName + "]" + e);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            throw new DBErrorException("jndi连接出错：jndiname:[" + jndiName + "]" + e);
        }
    }
    @Override
    public void close()
        throws DBErrorException
    {
        try
        {
            stmt.close();
            connection.close();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            throw new DBErrorException("connection close error \n" + e);
        }
    }
    @Override
    public ResultSet getResultBySql(String sqlString) throws DBErrorException {
        ResultSet rs = null;
        try
        {
            stmt = connection.createStatement();
            rs = stmt.executeQuery(sqlString);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            throw new DBErrorException("执行SQL语句：[" + sqlString + "]出错\n" + e);
        }
        return rs;
    }
    @Override
    public void execSql(String sql) throws DBErrorException{
        try{
            stmt.execute(sql);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            throw new DBErrorException("执行SQL语句：[" + sql + "]失败\n" + e);
        }
    }
    @Override
    public void setAutoCommit(boolean flag)
    {
        try
        {
            connection.setAutoCommit(flag);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    public abstract void openbyJDBC(String s, String s1, String s2)
        throws DBErrorException;
}
